﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using System.Globalization;

namespace Eugenhome.Atrium.Site
{
    /// <summary>
    /// 
    /// </summary>
    [XmlInclude(typeof(ImageItem))]
    public class ImageItemCollection
    {
        /// <summary></summary>
        public List<ImageItem> DefaultItems = new List<ImageItem>();

        /// <summary></summary>
        public List<ImageItem> Items = new List<ImageItem>();

        /// <summary></summary>
        [XmlIgnore]
        public ImageItem DefaultImageItem { get; private set; }

        /// <summary></summary>
        private string _dataFullFileName = "dayicons.xml";//Path.GetFullPath("dayicons.xml");

        /// <summary></summary>
        [XmlIgnore]
        public string DataFullFileName { get { return _dataFullFileName; } set { _dataFullFileName = value; } }

        /// <summary></summary>
        [XmlIgnore]
        public string WikiDayHref
        {
            get
            {
                CultureInfo ci = new CultureInfo("de-DE");
                string month = ci.DateTimeFormat.MonthNames[DAO.ClientDateTimeNow.Month - 1];
                return string.Format("http://de.wikipedia.org/wiki/{0}._{1}", DAO.ClientDateTimeNow.Day, month);
            }
        }

        /// <summary></summary>
        private DAO _dao = new DAO();

        /// <summary></summary>
        [XmlIgnore]
        public List<string> Errors = new List<string>();

        /// <summary>
        /// 
        /// </summary>
        public ImageItemCollection()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public void Load()
        {
            ImageItemCollection loadedobj = (ImageItemCollection)_dao.Load(DataFullFileName, this.GetType(), "");
            Errors.AddRange(_dao.Errors);

            loadedobj.DataFullFileName = DataFullFileName;

            DefaultItems = loadedobj.DefaultItems.OrderBy(item => item.Day).ToList();
            Items = loadedobj.Items.OrderBy(item => item.Day).ToList();
            DefaultImageItem = DefaultItems[0];

            DefaultItems.ForEach(item => item._parentCollection = this);
            Items.ForEach(item => item._parentCollection = this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ImageItem GetTodayImage()
        {
            string today = DAO.ClientDateTimeNow.ToString("MMdd");

            ImageItem imageItem = Items.Where(item => item.Day != null && item.Day.Equals(today)).FirstOrDefault();

            return imageItem == null ? DefaultImageItem : imageItem;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="imageItem"></param>
        public void Add(ImageItem imageItem)
        {
            imageItem._parentCollection = this;
            imageItem.ID = Items.Max(item => item.ID) + 1;

            Items.Add(imageItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bookmark"></param>
        public void Delete(ImageItem bookmark)
        {
            Delete(bookmark.ID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            ImageItem bookmark = Items.Where(item => item.ID.Equals(id)).FirstOrDefault();
            if (bookmark != null)
            {
                Items.Remove(bookmark);
                Save();
                Load();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        internal void Save()
        {
            _dao.Save(DataFullFileName, this, "");
            Errors.AddRange(_dao.Errors);
        }
    }
}
